const db = wx.cloud.database();
const myPlace=db.collection("myPlace");
Page({

  /**
   * 页面的初始数据
   */
  data: {
  areaList:{},
  show: false,
  myPlaceId:''
  
  },

  onLoad: function (options) {
    db.collection('region').limit(1).get()
      .then(res => {
        if (res.data && res.data.length > 0) {
          console.log(res.data[0].areaList)
          this.setData({
            areaList: res.data[0].areaList
          });
        }
      })
      .catch(err => {
        console.log(err);
      });
    //console.log(this.data.areaList)
    if(options.id){
      myPlace.doc(options.id).get().then(res=>{
        console.log(res.data)
        this.setData({
          myPlaceId:options.id,
          userName:res.data.userName,
          userNumber:res.data.userNumber,
          chooseAreaValue:res.data.userArea,
          userDetailPlace:res.data.userDetailPlace
        })
      })
    }
  },
  //显示弹出框
  showPopup:function() {
    this.setData({ show: true });
  },
  //隐藏弹出框
  onClose:function(){
    this.setData({ show: false });
  },
  //弹出框确认
  onConfirm:function(event){
    console.log(event.detail.values[0].name + " " + event.detail.values[1].name + " " + event.detail.values[2].name);
    var chooseAreaValue = event.detail.values[0].name + " " + event.detail.values[1].name + " " + event.detail.values[2].name;
    this.setData({ 
      show: false,
      chooseAreaValue: chooseAreaValue
    });
  },
  //弹出框取消
  onCancel:function(event){
    console.log(event.detail);
    this.setData({ show: false });
  },
  inputName:function(event){
    this.setData({
      userName:event.detail.value
    })
    console.log(event.detail)
  },
  inputNumber: function (event) {
    this.setData({
      userNumber: event.detail.value
    })
  },
  inputPlace: function (event) {
    this.setData({
      userDetailPlace: event.detail.value
    })
  },
  processUserInfo:function(){
    console.log(this.data.userName)
    if(this.data.userName==null||this.data.userName==undefined){
      wx.showToast({
        title: '收件人不能为空!',
        icon: "none",
        duration:2000
      })
      return false;
    }else if(this.data.userNumber==null||this.data.userNumber==undefined){
      wx.showToast({
        title: '收件人号码不能为空!',
        icon: "none",
        duration:2000
      })
      return false;
    }else if(this.data.chooseAreaValue==null||this.data.chooseAreaValue==undefined){
      wx.showToast({
        title: '请选择正确的收货地址!',
        icon: "none",
        duration:2000
      })
      return false;
    }else if(this.data.userDetailPlace==null||this.data.userDetailPlace==undefined){
      wx.showToast({
        title: '请输入详细地址！',
        icon: "none",
        duration:2000
      })
      return false;
    }
    return true;
  },
  onSave: function (event) {
    var infoTemp=this.processUserInfo();
    console.log(infoTemp);
    if(infoTemp==true){
      if(this.data.myPlaceId){
        wx.cloud.callFunction({
          name:'updatePlace',
            data:{
              id:this.data.myPlaceId,
              userName: this.data.userName,
              userNumber: this.data.userNumber,
              userPlace: this.data.chooseAreaValue +' '+this.data.userDetailPlace,
              userArea:this.data.chooseAreaValue,
              userDetailPlace:this.data.userDetailPlace
            }
        })
      }else{
        wx.cloud.callFunction({
          name:'addPlace',
          data:{
            userName: this.data.userName,
            userNumber: this.data.userNumber,
            userPlace: this.data.chooseAreaValue +' '+this.data.userDetailPlace,
            userArea:this.data.chooseAreaValue,
            userDetailPlace:this.data.userDetailPlace
          }
        })
      }
      wx.redirectTo({
        url: '../myPlace' 
      })
    }
    
  },
  onDelete:function(event){
    wx.showModal({
      title: '删除',
      content: '确认删除该收货地地？',
      success :(res=> {
        if (res.confirm) {
          console.log(this.data.myPlaceId)
          myPlace.doc(this.data.myPlaceId).remove().then(res=>{
            console.log(res)
          })
          wx.redirectTo({
            url: '../myPlace',
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      })
    })
    
   
  }
})
